var loginName = document.getElementById("userName");
var loginPwd = document.getElementById("passWord");
var access_token = "";
var tuichu = "再按一次退出程序";
var backButtonPress = 0;
var imageDiv = document.getElementById("imageDiv");
var content = document.getElementById("content");

(function(e) {

    mui.init({
        keyEventBind: {
            backbutton: true //开启back按键监听
        }
    });

    mui.plusReady(function() {
        // 关闭所有无用窗口
        clearAllWindows();
        plus.navigator.setStatusBarBackground('#3E7FC3');

        var userInfo = getLocalUserInfo();

        if(isEmpty(userInfo)) {
            plus.navigator.setStatusBarBackground('#31BA25');
            hiddenEle(imageDiv);
            showEle(content);
        } else {
            pageChange({
                pageUrl: "index.html",
                createNew: false,
                postData: {
                    pageflag: 'login.html'
                },
                aniShow: "fade-in"
            });
        }

        mui.back = function(event) {
            backButtonPress++;
            if(backButtonPress > 1) {
                plus.runtime.quit();
            } else {
                plus.nativeUI.toast(tuichu);
            }
            setTimeout(function() {
                backButtonPress = 0;
            }, 1000);
            return false;
        };

    });

})(mui);

$("#loginBtn").click(function() {
    getToken();
});
//登录
function getToken() {
    var username = loginName.value.trim();
    var passWord = loginPwd.value.trim();

    if(!username) {
        mui.toast('请输入用户名');
        return;
    }
    if(!passWord) {
        mui.toast('请输入密码');
        return;
    }
    crossDomainAjax({
        url: apiUrl,
        pastDate: {
            "op": "Phone_Login",
            "Login": username,
            "PassWord": passWord
        },
        nativeFlg: false,
        requestType: 'POST'
    }, function (result) {
        if(result.status == 0){
            mui.toast(result.Message);
            return;
        }
        
        console.log('Login: ');
        userInfo = result;
        console.log(JSON.stringify(result));
        localStorage.setItem('user_info', JSON.stringify(result.Main));
        pageChange({
            pageUrl: "index.html",
            createNew: false,
            postData: {
                pageflag: 'login.html'
            },
            aniShow: "fade-in"
        });
    });
}
